package com.example.campuslink.ui.activity.search

import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.GridLayoutManager
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.SearchInvitationAdapter
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.databinding.ActivitySearchResultBinding
import com.example.campuslink.ui.activity.BaseActivity
import com.example.campuslink.viewModel.search.SearchResultViewModel

class SearchResultActivity : BaseActivity<ActivitySearchResultBinding, SearchResultViewModel>(
    SearchResultViewModel::class.java,
    R.layout.activity_search_result,
    BR.viewModel
) {
    private var adapter: SearchInvitationAdapter? = null
    override fun initView() {
        initData()
        mViewDataBinding?.rvInvitationShow?.adapter = adapter
        mViewDataBinding?.rvInvitationShow?.layoutManager = GridLayoutManager(this, 2)
        mViewModel?.schoolAllInvitation?.observe(this) {
            adapter?.setListData(it.reversed())
        }
        mViewDataBinding?.ivBack?.setOnClickListener {
            finish()
        }
        mViewDataBinding?.etSearchContent?.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_SEND) {
                // 获取输入内容并发送请求
                val content = mViewDataBinding?.etSearchContent?.text.toString()
                mViewModel?.getInvitation(content)
                mViewModel?.addSearchInfo(content)
                true // 表示已处理该事件
            } else {
                false // 未处理其他动作
            }
        }
    }

    private fun initData() {
        val content = intent.getStringExtra(putExtraKey.SEARCH_WORD.key) ?: ""
        mViewModel?.context = this
        mViewModel?.initDB(this)
        mViewModel?.getInvitation(content)
        adapter = SearchInvitationAdapter(mViewModel!!)
    }
}